package org.elsfs.cloud.common.mybatis.ext.sql.support.select;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import lombok.Getter;
import org.elsfs.cloud.common.util.lang.NamingCase;
import org.elsfs.cloud.common.util.lang.StringUtils;

import java.util.regex.Pattern;

/**
 * 表达式选择项实现
 *

 */
@Getter
public class ExpressionSelectItem implements SelectItem {

    /**
     * 字段别名分割正则模式
     */
    private static final Pattern FIELD_ALIAS_SPLITTING_PATTERN = Pattern.compile("( +as +(?=\\w+$))|( +(?=\\w+$))", Pattern.CASE_INSENSITIVE);

    /**
     * 字段表达式
     */
    private final String expression;

    /**
     * 字段名别
     */
    private final String fieldAlias;

    public ExpressionSelectItem(String expression) {
        if (StringUtils.isBlank(expression)) {
            throw new MybatisPlusException( "选择字段表达式不能为空");
        }

        String[] tokens = FIELD_ALIAS_SPLITTING_PATTERN.split(expression);
        if (tokens.length == 0) {
            throw new MybatisPlusException( "选择字段表达式不能为空");
        }

        if (tokens.length == 1) {
            this.expression = tokens[0];
            this.fieldAlias = null;
            return;
        }

        this.expression = tokens[0];
        this.fieldAlias = tokens[1];
    }

    /**
     * 构建SQL片段
     *
     * @return SQL片段
     */
    @Override
    public String buildSqlFragment() {
        StringBuilder stringBuilder = new StringBuilder();
        if (StringUtils.isBlank(this.fieldAlias)) {
            stringBuilder.append(NamingCase.toUnderlineCase(this.expression));
        } else {
            stringBuilder.append(NamingCase.toUnderlineCase(this.expression))
                .append(" AS ")
                .append(NamingCase.toUnderlineCase(this.fieldAlias));
        }

        return stringBuilder.toString();
    }
}
